//
//  EmoticonManager.m
//  WLEmotionKeyBoard
//
//  Created by wanglei on 16/12/8.
//  Copyright © 2016年 wanglei. All rights reserved.
//

#import "EmoticonManager.h"

@implementation EmoticonManager

static EmoticonManager *emoticonManager = nil;
- (instancetype)init{
    self = [super init];
    if(self){
        NSMutableArray <EmoticonPackages *>*packages = [[NSMutableArray alloc]init];
        self.packages = packages;
    [self loadEmotions];
    }
    return self;
}

//单例设计模型
- (EmoticonManager *)sharedEmoticoManager;{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        emoticonManager = [[EmoticonManager alloc] init];
    });
    return emoticonManager;
}
//加载表情模型数组
- (void)loadEmotions{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"emoticons" ofType:@"plist" inDirectory:@"Emoticons.bundle"];
    if(path == nil){
        return;
    }
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSArray *array = dict[@"packages"];
    if (array == nil) {
        return;
    }
    for (NSDictionary *item in array) {
        NSString *id = item[@"id"];
        [self loadGroupEmoticon:id];
    }
}

- (void)loadGroupEmoticon:(NSString *)id{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist" inDirectory:[NSString stringWithFormat:@"Emoticons.bundle/%@",id]];
    NSLog(@"id = %@,path = %@",id,path);
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSString *group_name_cn = dict[@"group_name_cn"];
//    NSLog(@"group_name_cn = %@",group_name_cn);
//    获得分组表情的字典类型数组
    NSArray *emoticons = dict[@"emoticons"];
//    实例化emoticons模型
    EmoticonPackages *p = [[EmoticonPackages alloc]initWithid:id name:group_name_cn array:emoticons];
    [self.packages addObject:p];
    NSLog(@"packages = %@",self.packages);
    
}

//查找字符串中表情字符串转换为属性文本
- (NSAttributedString *)getImageTextWithEmoticonText:(NSString *)str font:(UIFont*)font{
    
   NSString *pattern = @"\\[.*?\\]";
NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
//    正则表达试的核心方法
    NSArray<NSTextCheckingResult *> *result = [regex matchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)];
    //2.遍历查找的结果 使用每一个表情字符串 去 表情模型中 匹配对应表情模型
    //使用倒叙遍历查询的结果集
    NSMutableAttributedString *strM = [[NSMutableAttributedString alloc]initWithString:str];
    NSInteger index = result.count - 1;
    if (index >= 0) {
        NSTextCheckingResult *r = result[index];
        NSRange range = [r rangeAtIndex:0];
        NSString *subStr = [str substringWithRange:range];
        //使用表情字符串 在表情数组中 去匹配对应的表情模型
        Emoticon *Em = [self getEmoticonWithEmoticonText:subStr];
        if (Em != nil) {
            NSAttributedString *imageText = [EmoticonAttachment emoticonImageToImageText:Em font:font];
            [strM replaceCharactersInRange:range withString:imageText];
        }
        index --;
    }
    return strM;
}

- (Emoticon *)getEmoticonWithEmoticonText:(NSString *)subStr{
    
    for (EmoticonPackages *p in self.packages) {
        
       self.emoticons = [p.emoticonList filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(Emoticon *em, NSDictionary<NSString *,id> * _Nullable bindings) {
           if (em.chs == subStr) {
               return YES;
           }
               return NO;
           
       }]];
        if (self.emoticons.count != 0) {
            return [self.emoticons lastObject];
        }
    }
  return nil;
}


@end
